#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
获取仓库信息
@author:stone
@file:get_repo.py
@time:2021/08/15
"""
import json
import math
import os
import time

from data_source.gitee import Gitee
from data_source.github import Github


# curl -X GET --header 'Content-Type: application/json;charset=UTF-8' -H "Authorization: token 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" 'https://api.github.com/search/repositories?q=stars%3A>%3D100+blockchain+sort%3Astars-desc&page=1&per_page=10'
#   stars:>=100+blockchain+sort:stars-desc

# curl -X GET --header 'Content-Type: application/json;charset=UTF-8' -H "Authorization: token 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" 'https://api.github.com/search/repositories?q=topic%3Ablockchain+stars%3A>%3D100+blockchain+sort%3Astars-desc&page=1&per_page=10'
#    topic:blockchain+stars:>=100+blockchain+sort:stars-desc


def git_github():
    # ["blockchain", "corda", "fabric+blockchain", "ipfs", "quorum", "cita+blockchain", "hyperledger", "eos", "bcos", "xuperchain", "ethereum", "neo", "bitcoin", "polkadot", "cosmos", "diem"] # keyword
    key_word = ["diem"]  # topic
    for key in key_word:
        # data_path = "data" + os.sep + "github" + os.sep + "topic-" + key + "_star100+"
        # data_path = "data" + os.sep + "github" + os.sep + "topic-" + key
        data_path = "data" + os.sep + "github" + os.sep + key
        header_file = data_path + os.sep + "header.json"

        github = Github()
        # query_words = "topic%3A" + key + "+stars%3A>%3D100+sort%3Astars-desc"
        # query_words = "topic%3A" + key + "+sort%3Astars-desc"
        query_words = key + "+sort%3Astars-desc"
        result = github.getRepoBySearch(query_words, "1", "100", "desc")
        if not os.path.exists(data_path):
            os.makedirs(data_path)

        # 保存Header
        with open(header_file, "w") as f:
            json.dump(dict(result.headers), f)
            j_str = json.loads(result.text)
            total_count = j_str["total_count"]

        total_page = math.ceil(total_count / 100)
        if total_page > 10:
            total_page = 10
        # 保存所有仓库信息
        for page in range(1, total_page + 1):
            content_file = data_path + os.sep + "{:0>3d}".format(page) + "github.json"
            # if os.path.exists(content_file):
            #     os.remove(data_path)
            with open(content_file, "a") as f:
                result = github.getRepoBySearch(query_words, str(page), "100", "desc")
                while result.status_code != 200:
                    print("Bad Request, status code:" + str(result.status_code))
                    print("   Got Response: " + result.text)
                    time.sleep(1)
                    result = github.getRepoBySearch(query_words, str(page), "100", "desc")

                print("Got Page: " + str(page))
                f.write(result.text)


def get_gitee():
    data_path = "data" + os.sep + "gitee"
    header_file = data_path + os.sep + "header.json"

    gitee = Gitee()
    result = gitee.getRepoBySearch("blockchain", "1", "100", "desc")
    if not os.path.exists(data_path):
        os.makedirs(data_path)

    # 保存Header
    with open(header_file, "w") as f:
        json.dump(dict(result.headers), f)
        total_page = result.headers.get("total_page")

    for page in range(1, int(total_page) + 1):
        content_file = data_path + os.sep + "{:0>3d}".format(page) + "gitee.json"
        if os.path.exists(content_file):
            os.remove(data_path)

        # 保存所有仓库信息
        with open(content_file, "a") as f:
            result = gitee.getRepoBySearch("blockchain", str(page), "100", "desc")
            print("Got Page: " + str(page))
            f.write(result.text)


"""
gitlab 接口不稳定，难以获取数据
"""


# def git_gitlab():
#     key_word = ["blockchain"]
#     for key in key_word:
#         data_path = "data" + os.sep + "gitlab" + os.sep + key
#         gitlab = Gitlab()
#         if not os.path.exists(data_path):
#             os.makedirs(data_path)
#
#         page = "1"
#         next_page = "1"
#         while len(next_page) > 0:
#             result = gitlab.getRepoBySearch(key, "projects", page, "100", "desc")
#             if result.status_code != 200:
#                 print("Response Code: {}, {}. Request Error!!!".format(result.status_code, result.reason))
#                 return
#             # 从header中获取下一页页码
#             json.dumps(dict(result.headers))
#             next_page = result.headers.get("X-Next-Page")
#             content_file = data_path + os.sep + "{:0>3d}".format(int(page)) + "gitlab.json"
#
#             # 保存所有仓库信息
#             f = open(content_file, "w")
#             f.write(result.text)
#             f.close()
#             print("Got Page: {}, Next Page: {}".format(str(page), str(next_page)))
#             page = next_page
#             time.sleep(1)


def get():
    git_github()
    get_gitee()
    # git_gitlab() gitlab 接口不稳定，难以获取数据
